var mongoose=require('mongoose');
var Post=mongoose.model('Post',{
    title:String,
    desc:String,
    author:{
        name:String,
        age:Number
    },
    comments:[
        {
            content:String,
            like:Number
        }
    ],
    slice:Array
});
var db=mongoose.connect('mongodb://localhost/MongoDB_QuanWei_2');

Post.update({
    title:'post 1'
},{
    author:{
        name:'Tom',
        age:20
    },
    comments:[
        {
            content:'c1',
            like:0
        }
    ]
},{upsert:true},(err,r)=>{
    console.log(err,r);

        Post.update({title:'post 1'},{
            $push:{
                comments:{
                    $each:[
                        {content:'c6',like:5},
                        {content:'c6',like:5},
                        {content:'c6',like:5},
                        {content:'c6',like:5}
                    ],
                    $slice:-3
                }
            }
        },(e,r)=>{
            console.log(e,r);//null { ok: 1, nModified: 1, n: 1 }
            process.exit(1);
        });


});

/*
 * $slice:n
 *
 * 参数n只能是负数：表示保留最后多少个(n)，可能用来固定数组长度
 * */





